package fr.castorhoz.isi_connectivity_widget.WifiWidget;

import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
import fr.castorhoz.isi_connectivity_widget.ConnBroadcastReceiver;
import fr.castorhoz.isi_connectivity_widget.ConnButton;
import fr.castorhoz.isi_connectivity_widget.R;

public class WifiBroadcastReceiver extends ConnBroadcastReceiver {


	public WifiBroadcastReceiver(ConnButton button) {
		super(button);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		final String action = intent.getAction();

		if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
			Log.i(this.getClass().toString(), "wifi changed");

			int info = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);

			// METTRE A JOUR LES IMAGES ET LES ETATS DES COMPONENTS
			RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
					R.layout.widget_layout);
			int selector = button.getSelectorUnknown();

			if (info == (WifiManager.WIFI_STATE_ENABLED)) {
				Log.i(this.getClass().toString(), "Status of wifi : connected");
				Toast.makeText(context.getApplicationContext(),
						"Status of wifi : enabled", Toast.LENGTH_SHORT).show();
				selector = button.getSelectorOn();
				button.setState(ConnButton.STATE_ON);
			} else if (info == (WifiManager.WIFI_STATE_ENABLING)) {
				Log.i(this.getClass().toString(), "Status of wifi : connecting");
				Toast.makeText(context.getApplicationContext(),
						"Status of wifi : enabling", Toast.LENGTH_SHORT).show();
				selector = WifiButton.selector_on;
				button.setState(ConnButton.STATE_ON);
			} else if (info == (WifiManager.WIFI_STATE_DISABLED)) {
				Log.i(this.getClass().toString(), "Status of wifi : disabled");
				Toast.makeText(context.getApplicationContext(),
						"Status of wifi : disconnected", Toast.LENGTH_SHORT)
						.show();
				selector = button.getSelectorOff();
				button.setState(ConnButton.STATE_OFF);
			} else if (info == (WifiManager.WIFI_STATE_DISABLING)) {
				Log.i(this.getClass().toString(), "Status of wifi : disabling");
				Toast.makeText(context.getApplicationContext(),
						"Status of wifi : disconnecting", Toast.LENGTH_SHORT)
						.show();
				selector = button.getSelectorOff();
			} else if (info == (WifiManager.WIFI_STATE_UNKNOWN)) {
				Log.i(this.getClass().toString(), "Status of wifi : unknown");
				Toast.makeText(context.getApplicationContext(),
						"Status of wifi : unknown", Toast.LENGTH_SHORT).show();
				selector = button.getSelectorUnknown();
			}
			remoteViews.setImageViewResource(button.getIdButton(), selector);

		}
	}

}
